package cn.bdqn.controller.sys;

import cn.bdqn.entity.Phone;
import cn.bdqn.service.sys.SysPhoneService;
import cn.bdqn.utils.MyLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * @ProjectName: ycbdqn
 * @Package: cn.bdqn.controller.sys
 * @Author: huat
 * @Date: 2020/3/12 17:46
 * @Version: 1.0
 */
@Controller
@RequestMapping("/sys/phone")
public class SysPhoneController {
    @Autowired
    SysPhoneService sysPhoneService;
    /**
     * 进入电话页面
     * @return
     */
    @RequestMapping("intoPhone")
    public String intoPhone(){
        return "phone/phone";
    }

    /**
     * 分页查询电话
     * @param page 起始位置
     * @param limit 页面大小
     * @param phoneNumber 电话
     * @param phoneName 电话归属人
     * @return
     */
    @RequestMapping("getAllPhone")
    @ResponseBody
    @MyLog("查询所有咨询电话")
    public Map<String,Object> getAllPhone(int page, int limit, String phoneNumber,String phoneName){
        return sysPhoneService.getAllPhone(page, limit, phoneNumber, phoneName);
    }

    /**
     * 进入修改电话界面
     * @param id 电话id
     * @return
     */
    @RequestMapping("intoUpdatePhone")
    public String intoUpdatePhone(int id, Model model){
        model.addAttribute("phone",sysPhoneService.getPhoneById(id));
        return "phone/updatePhone";
    }

    /**
     * 修改咨询电话
     * @param phone
     * @return
     */
    @RequestMapping("updatePhone")
    @ResponseBody
    @MyLog("修改咨询电话")
    public String updatePhone(Phone phone){
        if("".equals(phone.getPhoneNumber())||null==phone.getPhoneNumber()||phone.getPhoneNumber().length()!=11){
            return "3";
        }
        return sysPhoneService.updatePhone(phone);
    }

    /**
     * 根据id删除手机号
     * @param id
     * @return
     */
    @RequestMapping("deletePhone")
    @ResponseBody
    @MyLog("删除手机号")
    public boolean deletePhone(int id){
        return sysPhoneService.deletePhone(id);
    }
    /**
     * 进入添加页面
     */
    @RequestMapping("intoAddPhone")
    public String intoAddPhone(){
        return "phone/addPhone";
    }
    /**
     * 进入添加页面
     */
    @RequestMapping("addPhone")
    @ResponseBody
    @MyLog("添加手机号")
    public String addPhone(Phone phone){
        if("".equals(phone.getPhoneNumber())||null==phone.getPhoneNumber()||phone.getPhoneNumber().length()!=11){
            return "3";
        }
        return sysPhoneService.addPhoneNumber(phone);
    }
}
